package com.hooware.controller;

import com.hooware.domain.Permission;
import com.hooware.domain.Role;
import com.hooware.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/findAll.do")
    public String findAll(Model model) {
        List<Role> roleList = roleService.findAll();
        model.addAttribute("roleList",roleList);
        System.out.println("============查询所有角色信息===========" + roleList);
        return "role-list";
    }

    @RequestMapping("/save.do")
    public String save(Role role) {
        roleService.saveRole(role);
        System.out.println("============保存角色信息===========" + role);
        return "redirect:findAll.do";
    }

    @RequestMapping("/findById.do")
    public String  findById(Model model,String id) {
        Role role = roleService.findById(id);
        model.addAttribute("role",role);
        //model.addAttribute("permission",permission);
        System.out.println("============根据id查询角色信息===========" + role + " / " + role);
        return "role-show";
    }

    @RequestMapping("/findRoleByIdAndAllPermission.do")
    public String  findRoleByIdAndAllPermission(Model model,String id) {
        Role role = roleService.findById(id);
        List<Permission> permissionList = roleService.findOtherPermission(id);
        model.addAttribute("role",role);
        model.addAttribute("permissionList",permissionList);
        System.out.println("============根据id查询角色和权限信息===========" + role + " / " + permissionList);
        return "role-permission-add";
    }

    @RequestMapping("/addPermissionToRole.do")
    public String  addPermissionToRole(@RequestParam(name = "roleId", required = true) String roleId, @RequestParam(name = "ids", required = true) String[] pids) {
        roleService.addPermissionToRole(roleId,pids);
        return "redirect:findAll.do";
    }
}
